package com.sykuku.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sykuku.common.BaseContext;
import com.sykuku.common.R;
import com.sykuku.controller.expection.BusinessException;
import com.sykuku.pojo.OrderDetail;
import com.sykuku.pojo.ShoppingCart;
import com.sykuku.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class shoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //购物车展示
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getThreadLocalID());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        log.info("添加购物车数据:{}", shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getThreadLocalID());
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getThreadLocalID());
        if (setmealId != null) {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            // 添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart oneShopCar = shoppingCartService.getOne(queryWrapper);
        if (oneShopCar != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = oneShopCar.getNumber();
            oneShopCar.setNumber(number + 1);
            shoppingCartService.updateById(oneShopCar);
        }else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            oneShopCar = shoppingCart;
        }

        return R.success(oneShopCar);
    }

    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {

        log.info("减少购物车数据:{}", shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getThreadLocalID());
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getThreadLocalID());
        if (setmealId != null) {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            // 添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        //查询当前菜品或者套餐在购物车中的值
        ShoppingCart oneShopCar = shoppingCartService.getOne(queryWrapper);

        //如果不存在，返回错误信息
        if (oneShopCar==null){
            throw new BusinessException(1,"已经被清除了哦");
        }
        else {
        //如果只有一个值,清除数据库中的数据
            Integer number = oneShopCar.getNumber();
            if (number==1){
                shoppingCartService.removeById(oneShopCar);
            }
            //如果多个订单修改数量
            oneShopCar.setNumber(number-1);
            shoppingCartService.updateById(oneShopCar);
        }

        return R.success(oneShopCar);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        log.info("用户清除了所有购物车");
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getThreadLocalID());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }


}
